package com.dawn.login.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dawn.login.service.LoginService;
import com.dawn.usermanage.bean.UserBean;
import com.dawn.usermanage.bean.UserPreference;
import com.dawn.usermanage.service.UserManageService;
import com.opensymphony.xwork2.ActionSupport;

@Controller("loginAction")
@Scope("prototype")
public class LoginAction extends ActionSupport implements SessionAware,
		ServletRequestAware {

	private static final long serialVersionUID = -1897649005162084989L;
	private static Logger logger = Logger.getLogger(LoginAction.class);

	@Autowired
	private LoginService loginService;

	@Autowired
	private UserManageService userManageService;

	private Map<String, Object> session;
	private HttpServletRequest request;
	private UserBean user;
	private UserPreference userPreference;

	public UserBean getUser() {
		return user;
	}

	public void setUser(UserBean user) {
		this.user = user;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String sinaLogin() {
		String sinaId = session.get("sinaId").toString();
		UserBean userBean = userManageService.findUserBySinaId(sinaId);
		if (userBean == null) {
			return "register";
		}
		return SUCCESS;
	}

	public String login() {
		logger.info("---------Testing---------");
		UserBean userBean = new UserBean();
		userPreference = new UserPreference();
		if ("sina".equals(request.getParameter("type"))) {
			String sinaId = session.get("sinaId").toString();
			userBean = userManageService.findUserBySinaId(sinaId);
		}
		if(userBean == null)
			return ERROR;

 
        userPreference.setUserId(userBean.getUserId());
        userPreference.setMemberType(userBean.getMemberType());
        userPreference.setUserSinaName(userBean.getTwSinainfo().getScreenName());
        userPreference.setUserName(userBean.getUserName());
        userPreference.setImgUrl(userBean.getTwSinainfo().getProfileImg());
        userPreference.setTags(userBean.getTwSinainfo().getTags());
        session.put("user", userPreference);
 
		userPreference.setUserId(userBean.getUserId());
		userPreference.setMemberType(userBean.getMemberType());
		userPreference
				.setUserSinaName(userBean.getTwSinainfo().getScreenName());
		userPreference.setUserName(userBean.getUserName());
		userPreference.setImgUrl(userBean.getTwSinainfo().getProfileImg());
		
		session.put("user", userPreference);
 
		loginService.updateUserSinaInfo(userBean.getTwSinainfo().getSinaId());
		
		logger.info(userBean.getUserName() + ":" + userBean.getUserId()
				+ "login the system.");
		return SUCCESS;
	}

	// public String login() {
	// logger.info("---------Testing---------");
	//
	// UserBean userBean = new UserBean();
	// userPreference = new UserPreference();
	// if ("sina".equals(request.getParameter("type"))) {
	// String sinaId = session.get("sinaId").toString();
	// userBean = userManageService.findUserBySinaId(sinaId);
	// if (userBean == null) {
	// return "register";
	// }
	// } else {
	// userBean = loginService.findUser(user.getUserName(), user.getPassword());
	// if (userBean == null) {
	// return ERROR;
	// }
	// }
	//
	// userPreference.setUserId(userBean.getUserId());
	// userPreference.setMemberType(userBean.getMemberType());
	// userPreference.setUserSinaName(userBean.getTwSinainfo().getScreenName());
	// userPreference.setUserName(userBean.getUserName());
	// userPreference.setImgUrl(userBean.getTwSinainfo().getProfileImg());
	// session.put("user", userPreference);
	//
	// loginService.updateUserSinaInfo(userBean.getTwSinainfo().getSinaId());
	//
	// logger.info(userBean.getUserName() + ":" + userBean.getUserId() +
	// "login the system.");
	//
	// return SUCCESS;
	//
	// }

}
